<?php
/****
* 活动照片模块, 处理图片的 上传\显示 相关
* TODO: 内训时临时加的, 有条件的话最好大幅重写= =
****/
class AlbumAction extends InitAction{
    static $COUNT_PER_PAGE=20;
    private $album=null;
    public function _initialize(){
        parent::_initialize();
        
        $this->aid= $_GET['id'];
        $this->type= $_GET['type'];
        $this->uid= $this->isLogged();
        if ('event'==$this->type){
            import('@.Model.Extend.EventAlbum');
            $this->album= new EventAlbum($this->aid, $this->uid);
        }else{
        //TODO 处理其他相册
            import('@.Model.Extend.EventAlbum');
            $this->album= new EventAlbum($this->aid);
        }
    }
    public function index(){
        $this->error("错误的参数");
    }
    /****
    * 显示相册内容
    * @id 关联条目的id
    */
    public function id(){
        //分页
        import("@.Common.NoticePage");
        $count=$this->album->getAlbumCount();
        $page= new NoticePage($count, self::$COUNT_PER_PAGE);
        $show= $page->show();
        $this->assign('page', $show);
        
        $this->assign('album',$this->album->getAlbum($page->firstRow, $page->listRows));
        $this->assign('event', $this->album->getEvent());
        $this->assign('allowUpload', $this->album->allowUpload());
        $this->display('index');
    }
    /****
    * 上传照片
    * @id 关联条目id
    * @type 内容
    */
    public function upload(){
        import('@.Common.NoticeImageUploader');
        if (! $_POST['submit']){
            if (! $this->album->allowUpload()){
                $this->error("没有上传权限");
            }
            $this->display('upload');
        } else{
        //处理上传
            if ($_FILES['imgfile']['size']<=1){
            //没有选择文件，则跳过
                $this->error("请先选择文件");
            }
            $uploader= new NoticeImageUploader();
            $res= $uploader->uploadEventImage();
            if(! $res) { // 上传错误 提示错误信息   
                $this->error($uploader->getErrorMsg());
                return false;
            }else{ // 上传成功 获取上传文件信息   
                $imginfo =  $uploader->getUploadFileInfo(); 
                $_POST['img']  = $imginfo[0]['savename'];            
            }
            
            if (! array_key_exists('descr', $_POST)){
                $descrText= '';
            } else{
                $descrText= $_POST['descr'];
            }
            $albumM= M('Album');
            $data=array(
                'type'=>$this->type,
                'aid'=>$this->aid,
                'uid'=> $this->isLogged(),
                'img'=> $imginfo[0]['savename'],
                'text'=> $descrText,
            );
            $res= $albumM->data($data)->add();
            if (false===$res){
                $this->error($albumM->getError());
            } else{
                $this->assign('jumpUrl', U("event/$this->aid/album"));
                $this->success("上传成功");
            }
        }
    }
    protected function getEventInfo($eid){
    }
    protected function getGroupInfo($gid){
    }
}
?>